<?php

class Default_OrderController extends Zend_Controller_Action
{

    public function init()
    {
        $option = array(
                'layout'=>'layout',
                'layoutPath'=>APPLICATION_PATH .'/layouts/scripts/default'
        );
        Zend_Layout::startMvc($option);
        $this->model_order = new Application_Model_Order();
        $this->model_order_detail = new Application_Model_Orderdetail();
    }

    public function indexAction()
    {
        try{           
                //get cart
                $cart = new Zend_Session_Namespace('cart');
                if($cart->item){
                    $form_order = new Default_Form_Order();
                    $this->view->form_order = $form_order;
                    $this->view->headTitle('Thông tin đặt hàng');
                    
                    
                    if($this->_request->isPost()){
                        if($form_order->isValid($this->_request->getPost())){
                            $date = new Zend_Date();
                            $order_id = $date->setTimezone('Asia/Ho_Chi_Minh')->toString('yyyyMMddHHmmssS');
                            $name = $form_order->getValue('order_guessname_delivery');
                            $address = $form_order->getValue('order_guessaddress_delivery');
                            $phone = $form_order->getValue('order_guessphone_delivery');
                            $mail = $form_order->getValue('order_guessemail_delivery');
                    
                            $data_order = array(
                                    'order_id'=>$order_id,
                                    'order_datecreated'=>$date->setTimezone('Asia/Ho_Chi_Minh')->toString('yyyy-MM-dd HH:mm:ss'),
                                    'order_guessname_delivery'=>$name,
                                    'order_guessphone_delivery'=>$phone,
                                    'order_guessaddress_delivery'=>$address,
                                    'order_guessemail_delivery'=>$mail,
                                    'order_state'=>0,
                            );
                    
                            $this->model_order->insert($data_order);
                    
                            foreach ($cart->item as $item_cart){
                                $data_order_detail = array(
                                        'order_id'=>$order_id,
                                        'product_id'=>$item_cart['product']->product_id,
                                        'quantity'=>$item_cart['quantity'],
                                );
                                //insert data to order_detail
                                $this->model_order_detail->insert($data_order_detail);
                            }
                            //clear session cart
                            $cart->unsetAll();
                            $this->_redirect(array('module'=>'default', 'controller'=>'index', 'action'=>'index'));
                            
                        }
                    }
                }
                else {
                    $this->_helper->redirector('index', 'index', 'default');
                }
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
        
    }


}

